#!/bin/sh

function getKeyString()
{
    value=`echo $1|grep $2|sed "s/^.*$2\":\"//g"|sed "s/\".*$//g"`
    echo $value
}

function getKeyNumber()
{
    value=`echo $1|grep $2|sed "s/^.*$2\"://g"|sed "s/[,}].*$//g"`
    echo $value
}

http="https"
url="www.marsiot.com/marsiot/index.php?route=api"

#设备列表中可以看到设备的assignment token
assignmentToken='3cfc7fb3-f9c6-41cd-a7ff-6e6fed1bd354'

#在命令列表中可以看到命令的command token
commandToken='c84cfefe-9381-4e8b-b222-4567ef96cbb8'

parameters='[{"name":"greeting","type":"String","value":"test"},{"name":"loud","type":"Bool","value":"true"}]'

method="smartiot/sendcommand"
data="assignmentToken=$assignmentToken&commandToken=$commandToken&parameters=$parameters"

set -x
ret=`curl -k -s -POST -d $data "$http://$url/$method"`
set +x
echo $ret|grep "success"
if [ $? -eq 0 ]
then
    echo -e "\nsend command ok\n"
else
    exit 1
fi

#等待设备收到命令并发送消息，大多数情况等待3秒是够用的
echo -e "wait for message ...\n"
sleep 3

method="smartiot/listassignmentmessages"
data="[]"

set -x
ret=`curl -k -s  -POST -d $data "$http://$url/$method&assignmentToken=$assignmentToken&page=1&pageSize=1"`
set +x
echo $ret|grep "success"
if [ $? -eq 0 ]
then
    message=`getKeyString "$ret" 'message'`
    echo -e "\nmessage: $message\n"
else
    echo "failed"
fi


